using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web;
using System.Xml;
using System;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2004-08-10
	State:		dev
	Version:	0.0.1
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX.workflowX {
	/// <summary>
	/// The x_workstate class. 
	/// </summary>
	public class x_workstate : x_DbData {
		#region Constants
		private const string workstatexml = "<workflowX><workstate/></workflowX>";
		private const string error_get 			= "The state does not exist within the system.";
		private const string error_nullid		= "The state identifier is null.";
		#endregion

		#region Invisible properties
		private XmlDocument workstate;
		#endregion

		#region Visible properties
		/// <summary>The workstate definition</summary>
		public XmlElement WorkState {
			get { return (XmlElement) workstate.SelectSingleNode("/workflowX/workstate"); }
		}
		/// <summary>The collection of workstates</summary>
		public XmlElement States {
			get { 
				getStates();
				return (XmlElement) workstate.SelectSingleNode("/workflowX/workstate/workstates");
			}
		}
		private int id;
		/// <summary>ID property</summary>
		public int ID {
			get { return id; }
			set { 
				id = value;
				WorkState.SetAttribute("id", id.ToString());
			}
		}
		#endregion

		#region Constructors/Destructors
		/// <summary>Constructor</summary>
		/// <param name="DSN">the dsn (from web.config)</param>
		public x_workstate(string DSN) : base(DSN) {
			initialise();
		}
		#endregion

		#region Public methods
		///	<summary>
		///	Creates a work-status and saves it into the database
		///	</summary>
		public void Create(string Name, string Description) {
			_getCommand("xStateAdd", CommandType.StoredProcedure);
			_addDbParameter(Name, "@Name", 50);
			_addDbParameter(Description, "@Description", 200);
			IDataParameter parID = _addDbParameter("@ID");
			_executeNonQuery();
			ID = (int) _getParameter(parID);
		}
		///	<summary>
		///	Retreives a work-status from the database
		///	</summary>
		public void Get(int thisid) {
			ID = thisid;
			Get();
		}
		///	<summary>
		///	Retreives a work-status from the database
		///	</summary>
		public void Get() {
			if (id == 0)
				throw(new x_exception("error_nullid", error_nullid));
			else
				getState();
		}
		private void getState() {
			_getCommand("xStateGet", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ID");
			SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
			if(rdr.Read()) {
				_addAttribute(workstate, WorkState, "name", _getString(rdr, "Name"));
				_addElement(workstate, WorkState, "description", _getString(rdr, "Description"));
			} else {
				throw(new x_exception("error_get", error_get));
			}
			rdr.Close();
		}
		private void getStates() {
			_getCommand("xStateGet", CommandType.StoredProcedure);
			_addDbParameter(0, "@ID");
			SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
			XmlElement states = (XmlElement) WorkState.AppendChild(workstate.CreateElement("workstates"));
			while(rdr.Read()) {
				XmlElement state = (XmlElement) states.AppendChild(workstate.CreateElement("workstate"));
				_addAttribute(workstate, state, "id", _getString(rdr, "ID"));
				_addAttribute(workstate, state, "name", _getString(rdr, "Name"));
				_addElement(workstate, state, "description", _getString(rdr, "Description"));
			}
			rdr.Close();
		}
		///	<summary>
		///	Saves an existing work-process to the database
		///	</summary>
		public void Save(int thisid, string Name, string Description) {
			ID = thisid;
			_getCommand("xStateSave", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ID");
			_addDbParameter(Name, "@Name", 50);
			_addDbParameter(Description, "@Description", 200);
			_executeNonQuery();
		}
		#endregion
		
		#region Protected methods
		#endregion
				
		#region Private methods
		private void initialise() {
			workstate = new XmlDocument();
			workstate.LoadXml(workstatexml);
		}
		#endregion

	}
}
